package ca.jsbr.as2js_lib.display.canvas.view 
{
	import ca.jsbr.as2js_lib.utils.geom.Rect;
	import js.element.canvas.CanvasRenderingContext2D;
	import js.event.Event;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class Image9Grid extends Image 
	{
		private var _rect:Rect;
		private var _grid:Vector.<Rect>;
		
		public function Image9Grid(src:*=null) 
		{
			_grid = new Vector.<Rect>();
			super(src);
		}
		
		override protected function onLoad(e:Event):void 
		{
			super.onLoad(e);
			if(!_rect)
				setRegion(new Rect(_srcWidth * 0.15, _srcHeight * 0.15, _srcWidth - _srcWidth * 0.3, _srcHeight - _srcHeight * 0.3));
			
		}
		
		public function setRegion(rect:Rect):void
		{
			_rect = rect;
			_grid[0] = new Rect(0, 0, _rect.x, _rect.y);
			_grid[1] = new Rect(_rect.x, 0, _rect.width, _rect.y);
			_grid[2] = new Rect(_rect.right, 0, _srcWidth-_rect.right, _rect.y);
			_grid[3] = new Rect(_rect.right, _rect.y, _srcWidth-_rect.right,_rect.height);
			_grid[4] = new Rect(_rect.right, _rect.bottom, _srcWidth-_rect.right, _srcHeight-_rect.bottom);
			_grid[5] = new Rect(_rect.x, _rect.bottom, _rect.width, _srcHeight-_rect.bottom);
			_grid[6] = new Rect(0, _rect.bottom, _rect.x, _srcHeight-_rect.bottom);
			_grid[7] = new Rect(0, _rect.y, _rect.x, _rect.width);
			_grid[8] = new Rect(_rect.x, _rect.y, _rect.width, _rect.height);
		}
		
		override public function render(context:CanvasRenderingContext2D):void 
		{
			if (_ready)
			{
				var width8:Number = _width - _grid[0].width - _grid[2].width;
				if (width8 < 0)
					width8 = 0;
				var height8:Number = _height - _grid[0].height - _grid[6].height
				if (height8 < 0)
					height8 = 0;
				
				var right:Number = stageX + width8 + _grid[0].width;
				var bottom:Number = stageY + _grid[3].y + height8;
				
				context.drawImage(_image, _grid[0].x, _grid[0].y, _grid[0].width, _grid[0].height, stageX, stageY, _grid[0].width, _grid[0].height);
				context.drawImage(_image, _grid[1].x, _grid[1].y, _grid[1].width, _grid[1].height, stageX + _grid[1].x, stageY,  width8, _grid[1].height);
				context.drawImage(_image, _grid[2].x, _grid[2].y, _grid[2].width, _grid[2].height, right, stageY,  _grid[2].width, _grid[2].height);
				context.drawImage(_image, _grid[3].x, _grid[3].y, _grid[3].width, _grid[3].height, right, stageY+_grid[3].y,  _grid[3].width, height8);
				context.drawImage(_image, _grid[4].x, _grid[4].y, _grid[4].width, _grid[4].height, right, bottom,  _grid[4].width, _grid[4].height);
				context.drawImage(_image, _grid[5].x, _grid[5].y, _grid[5].width, _grid[5].height, stageX + _grid[1].x, bottom,  width8, _grid[5].height);
				context.drawImage(_image, _grid[6].x, _grid[6].y, _grid[6].width, _grid[6].height, stageX, bottom,  _grid[6].width, _grid[6].height);
				context.drawImage(_image, _grid[7].x, _grid[7].y, _grid[7].width, _grid[7].height, stageX, stageY+_grid[0].height,  _grid[7].width, height8);
				context.drawImage(_image, _grid[8].x, _grid[8].y, _grid[8].width, _grid[8].height, stageX+_grid[0].width, stageY+_grid[0].height,  width8, height8);
			}
			
		}
		
	}

}